package com.douqu.game.main.util;

import com.bean.core.util.TimeUtils;
import com.douqu.game.core.config.PlayerLvConfig;
import com.douqu.game.core.config.card.CardConfig;
import com.douqu.game.core.config.card.CardLvConfig;
import com.douqu.game.core.config.challenge.ArenaRobotConfig;
import com.douqu.game.core.config.goods.AccessoryIntensifyConfig;
import com.douqu.game.core.config.sprite.MasterConfig;
import com.douqu.game.core.database.model.PlayerModel;
import com.douqu.game.core.e.E_LegionChinaType;
import com.douqu.game.core.entity.Player;
import com.douqu.game.core.entity.db.AccessoryDB;
import com.douqu.game.core.entity.db.CardDB;
import com.douqu.game.core.entity.db.EquipDB;
import com.douqu.game.core.entity.ext.data.card.CardData;
import com.douqu.game.core.entity.world.WorldArenaData;
import com.douqu.game.core.factory.ConstantFactory;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.protobuf.SGCommonProto;
import com.douqu.game.core.util.LoadUtils;
import com.douqu.game.core.util.LogUtils;
import com.douqu.game.main.GameServer;

import java.util.*;

/**
 * @author wangzhenfei
 *         2018-01-29 17:43
 *         机器人创建辅助类
 */
public class RobotCreateHelper {
    public static  RobotCreateHelper HELPER = null;
    private List<Integer> orangeCardIds;
    Random random;
    WorldArenaData worldArenaData;

    List<ArenaRobotConfig> robotConfigs;
    private RobotCreateHelper() {
        orangeCardIds = new ArrayList<>();
        random = new Random();
        worldArenaData = GameServer.getInstance().getWorldArenaData();
        List<CardConfig> cardConfigs = DataFactory.getInstance().getDataList(DataFactory.CARD_KEY);
        for(CardConfig config: cardConfigs){
            if(config.quality == ConstantFactory.QUALITY_ORANGE){
                orangeCardIds.add(config.id);
            }
        }
        robotConfigs = DataFactory.getInstance().getDataList(DataFactory.ARENA_ROBOT_KEY);

    }

    public static RobotCreateHelper getInstance(){
        if(HELPER == null){
            HELPER = new RobotCreateHelper();
        }
        return HELPER;
    }



    public  PlayerModel createPlayerByArenaPosition(int i){
        RobotCreateHelper helper = RobotCreateHelper.getInstance();
        SGCommonProto.E_CAMP_TYPE camp = helper.getCamp(i);
        String name = LoadUtils.getRandomPlayerName();
        int masterId = helper.getMaster(camp);


        int star = 0;
        int solderLv = 1;
        int equipLv = 1;
        int accessoryLv = 0;
        int accessoryRank = 0;
        int orangeCardNum = 0;

        for(ArenaRobotConfig robotConfig : robotConfigs){
            if(i <= robotConfig.id ){
                star = helper.getRandomFromTwoNum(robotConfig.star.type, robotConfig.star.id);
                solderLv = helper.getRandomFromTwoNum(robotConfig.soldierLv.type, robotConfig.soldierLv.id);
                equipLv = helper.getRandomFromTwoNum(robotConfig.equipLv.type, robotConfig.equipLv.id);
                accessoryLv = helper.getRandomFromTwoNum(robotConfig.accessoryLv.type, robotConfig.accessoryLv.id);
                accessoryRank = helper.getRandomFromTwoNum(robotConfig.accessoryRank.type, robotConfig.accessoryRank.id);
                orangeCardNum = helper.getRandomFromTwoNum(robotConfig.orangeCardNum.type, robotConfig.orangeCardNum.id);
                break;
            }
        }

//        if(i == 1){
//            star = 4;
//            solderLv = 40;
//            equipLv = 80;
//            accessoryLv = 7;
//            accessoryRank = 2;
//            orangeCardNum = 4;
//        }else if(i<11){
//            star = helper.getRandomFromTwoNum(3, 4);
//            solderLv = helper.getRandomFromTwoNum(35, 40);
//            equipLv = helper.getRandomFromTwoNum(70, 80);
//            accessoryLv = helper.getRandomFromTwoNum(5, 7);
//            accessoryRank = 2;
//            orangeCardNum = helper.getRandomFromTwoNum(3, 4);
//        }else if(i<31){
//            star = helper.getRandomFromTwoNum(3, 4);
//            solderLv = helper.getRandomFromTwoNum(33, 39);
//            equipLv = helper.getRandomFromTwoNum(64, 76);
//            accessoryLv = helper.getRandomFromTwoNum(5, 6);
//            accessoryRank = 2;
//            orangeCardNum = helper.getRandomFromTwoNum(3, 4);
//        }else if(i<41){
//            star = 3;
//            solderLv = helper.getRandomFromTwoNum(32, 38);
//            equipLv = helper.getRandomFromTwoNum(64, 76);
//            accessoryLv = helper.getRandomFromTwoNum(5, 6);
//            accessoryRank = 2;
//            orangeCardNum = 3;
//        }else if(i<51){
//            star = 3;
//            solderLv = helper.getRandomFromTwoNum(32, 38);
//            equipLv =helper.getRandomFromTwoNum(64, 76);
//            accessoryRank = 2;
//            accessoryLv = 5;
//            orangeCardNum = 3;
//        }else if(i<101){
//            star = helper.getRandomFromTwoNum(2, 3);
//            solderLv = helper.getRandomFromTwoNum(25, 35);
//            equipLv = helper.getRandomFromTwoNum(50, 70);
//            accessoryLv = helper.getRandomFromTwoNum(3, 5);
//            accessoryRank = 1;
//            orangeCardNum = helper.getRandomFromTwoNum(2, 3);
//        }else if(i<201){
//            star = helper.getRandomFromTwoNum(1, 3);
//            solderLv = helper.getRandomFromTwoNum(20, 35);
//            equipLv = helper.getRandomFromTwoNum(40, 70);
//            accessoryLv = helper.getRandomFromTwoNum(2, 4);
//            accessoryRank =1;
//            orangeCardNum = 2;
//        }else if(i<301){
//            star = helper.getRandomFromTwoNum(1, 3);
//            solderLv = helper.getRandomFromTwoNum(20, 30);
//            equipLv = helper.getRandomFromTwoNum(40, 60);
//            accessoryLv = helper.getRandomFromTwoNum(1, 4);
//            accessoryRank = 1;
//            orangeCardNum = 2;
//        }else if(i<401){
//            star = helper.getRandomFromTwoNum(1, 2);
//            solderLv = helper.getRandomFromTwoNum(15, 30);
//            equipLv = helper.getRandomFromTwoNum(30, 60);
//            accessoryLv = helper.getRandomFromTwoNum(1, 3);
//            accessoryRank = helper.getRandomFromTwoNum(0, 1);
//            orangeCardNum = 1;
//        }else if(i<501){
//            star = helper.getRandomFromTwoNum(0, 3);
//            solderLv = helper.getRandomFromTwoNum(15, 25);
//            equipLv = helper.getRandomFromTwoNum(30, 50);
//            accessoryLv = helper.getRandomFromTwoNum(0, 3);
//            accessoryRank = helper.getRandomFromTwoNum(0, 1);
//            orangeCardNum = 1;
//        }else if(i<1001){
//            star = helper.getRandomFromTwoNum(0, 2);
//            solderLv = helper.getRandomFromTwoNum(10, 20);
//            equipLv = helper.getRandomFromTwoNum(20, 40);
//            accessoryLv = helper.getRandomFromTwoNum(0, 2);
//            accessoryRank = 0;
//            orangeCardNum = 0;
//        }else if(i<1501){
//            star = helper.getRandomFromTwoNum(0, 1);
//            solderLv = helper.getRandomFromTwoNum(1, 15);
//            equipLv = helper.getRandomFromTwoNum(1, 30);
//            accessoryLv = helper.getRandomFromTwoNum(0, 1);
//            accessoryRank = 0;
//            orangeCardNum = 0;
//        }else if(i<2001){
//            star = 0;
//            solderLv = 1;
//            equipLv = 1;
//            accessoryLv = 0;
//            accessoryRank = 0;
//            orangeCardNum = 0;
//        }
        Player player = createPlayer(camp, name, masterId, star, solderLv, equipLv, accessoryLv, accessoryRank, orangeCardNum);
        return player.save();
    }


    /**
     *
     * @param camp 阵营
     * @param name 名字
     * @param masterId 主将id
     * @param star 卡片星级
     * @param solderLv 卡片等级和玩家等级
     * @param equipLv 装备等级
     * @param accessoryLv 饰品等级
     * @param accessoryRank 饰品升阶等级
     * @param orangeCardNum 给的橙卡数量
     * @return
     */
    public  Player createPlayer(SGCommonProto.E_CAMP_TYPE camp, String name, int masterId, int star,
                                int solderLv, int equipLv, int accessoryLv, int accessoryRank, int orangeCardNum) {

        Date currentDate = TimeUtils.getCurrentDate();
        Player player = new Player();
        player.setObjectIndex(GameServer.getInstance().createPlayerIndex());
        player.setUid(0);
        player.setName(name);
        player.avatar = camp.getNumber() + "";
        player.camp = camp.getNumber();
        player.master = masterId;
        player.init();
        player.setCreateTime(currentDate);
        player.setLastLoginTime(currentDate);

        CardData cardData = player.getCardData();
        if(orangeCardNum > 0){//添加橙卡
            List<Integer> ids = getOrangeCardByCount(orangeCardNum);
            if(ids != null){
                for(Integer id : ids){
                    cardData.addBattleCard(new CardDB(id, player));
                }
            }
        }

        //更新卡的装备和等级
        List<CardDB> battleCardList = cardData.getBattleCardList();
        Integer key;
        for(CardDB cardDB : battleCardList){
            cardDB.star = star;
            CardLvConfig cardLvConfig = DataFactory.getInstance().getGameObject(DataFactory.CARD_LV_KEY, solderLv);
            cardDB.addExp(cardLvConfig.getTotalExp(cardDB.getConfig().quality));
            //装备等级
            for(EquipDB equipDB : cardDB.getEquipMap().values())
            {
                equipDB.lvUp(equipLv);
            }

            //饰品等级和强化
            for(AccessoryDB accessoryDB : cardDB.getAccessoryMap().values())
            {
                accessoryDB.isActivated = true;
                AccessoryIntensifyConfig accessoryIntensifyConfig = DataFactory.getInstance().getGameObject(DataFactory.ACCESSORY_INTENSIFY_KEY, accessoryLv);
                if(accessoryIntensifyConfig != null){// 强化
                    accessoryDB.addExp(accessoryIntensifyConfig.getTotalExp(cardDB.getConfig().quality));
                }
                //升阶
                accessoryDB.upLv = accessoryRank;
            }
        }

        PlayerLvConfig playerLvConfig = DataFactory.getInstance().getGameObject(DataFactory.PLAYER_LV_KEY, solderLv);
        player.addExp(playerLvConfig.totalExp);
        player.getCardData().calculateLegionChinaResult(E_LegionChinaType.ALL);
        player.updateFC();
        LogUtils.debug("创造机器人　战斗力 -> " + player.fc + "  " + player.getName());
        return player;
    }


    /**
     * 获取橙色卡片数量
     * @param count
     * @return
     */
    public List<Integer> getOrangeCardByCount(int count){
        if(count <= orangeCardIds.size()){
            List<Integer> ids = new ArrayList<>();
            while (true){
                if(ids.size() >= count){
                    return ids;
                }
                int cardId = orangeCardIds.get(random.nextInt(orangeCardIds.size()));
                if(!ids.contains(cardId)){
                    ids.add(cardId);
                }
            }
        }
        return null;
    }

    public static int getMaster(SGCommonProto.E_CAMP_TYPE camp) {
        List<MasterConfig> masterConfigs = DataFactory.getInstance().getDataList(DataFactory.MASTER_KEY);
        for(MasterConfig masterConfig : masterConfigs)
        {
            if(masterConfig.camp == camp.getNumber())
            {
                return masterConfig.id;
            }
        }
        return 0;
    }

    public  SGCommonProto.E_CAMP_TYPE getCamp(int i) {
        switch (i % 10){
            case 0:
            case 3:
            case 6:
            case 9:
                return SGCommonProto.E_CAMP_TYPE.CAMP_TYPE_BRIGHT;
            case 1:
            case 4:
            case 7:
                return SGCommonProto.E_CAMP_TYPE.CAMP_TYPE_BLOODY;
            case 2:
            case 5:
            case 8:
                return SGCommonProto.E_CAMP_TYPE.CAMP_TYPE_DARK;
        }
        return null;
    }

    public  int getRandomFromTwoNum(int start, int end){
        if(start == end){
            return start;
        }
        return random.nextInt(end - start + 1) + start;
    }


    public void appendToArenaWorldRank(String objectIndex){
        worldArenaData.getArenaRankByObjectIndex(objectIndex);
    }

}
